package com.mrd.gtimp.business.entity;

import com.mrd.gtimp.base.entity.User;
import com.mrd.util.PkCleanerUtil;
import com.mrd.util.PkConfig;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;

@Entity(name = "T_BIZ_TRAIL_MODEL")
public class TrailModel extends BaseBusinessModel implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 装货信息
     */
    @OneToMany(mappedBy = "trail")
    @Cascade(value = {CascadeType.SAVE_UPDATE, CascadeType.DELETE})
    private List<GoodAddressModel> gdes;
    /**
     * 前置信息，海运订舱id和so存在一个即可
     */
    /**
     * so号码
     */
    @Column(name = "SO")
    private String so;

    /**
     * PI号
     */
    @Column(name = "PI")
    private String pi;

    /**
     * 柜总量
     */
    @Column(name = "CONTAINER_TOTLE")
    private String containerTotle;

    /**
     * 报关票数登记
     */
    @Column(name = "DECLARATION_COUNT")
    private String declarationCount;

    /**
     * 是否为常用模板
     */
    @Column(name = "COMMON_TEMPLATE")
    private String commonTemplate;

    /**
     * 交接单号
     */
    @Column(name = "REQ_NO")
    private String reqNo;
    /**
     * 反馈信息在装货信息的车内
     */
    /**
     * 拖车状态
     */
    @Column(name = "STATE")
    private String trailState;

    /**
     * 电子版SO
     */
    @OneToOne
    @JoinColumn(name = "SO_FILE_ID")
    private Attachment soDoc;

    /**
     * 常用模板名
     */
    @Column(name = "COMMON_TEMPLATE_NAME")
    private String commonTemplateName;

    public TrailModel(Trail trail) {
        this.gdes = new ArrayList<GoodAddressModel>();
        for (GoodAddress goodAddress : trail.getGdes()) {
            GoodAddressModel goodAddressModel = new GoodAddressModel(goodAddress);
            this.gdes.add(goodAddressModel);
        }
        this.so = trail.getSo();
        this.pi = trail.getPi();
        this.containerTotle = trail.getContainerTotle();
        this.declarationCount = trail.getDeclarationCount();
        this.commonTemplate = trail.getCommonTemplate();
        this.reqNo = trail.getReqNo();
        this.trailState = trail.getTrailState();
        this.commonTemplateName = trail.getCommonTemplateName();

		/* super.setId(trail.getId()); */
        super.setShipper(new ContacterModel(trail.getShipper()));
        super.setCustomer(trail.getCustomer());
        super.setCreator(trail.getCreator());
        super.setCtime(trail.getCtime());
        super.setModifier(trail.getModifier());
        super.setMtime(trail.getMtime());
        super.setBusinessId(trail.getBusinessId());
        super.setType(trail.getType());
        super.setSupCompany(trail.getSupCompany());
        super.setRealSupId(trail.getRealSupId());
        super.setBpk(trail.getBpk());
        super.setConfirmTime(trail.getConfirmTime());
        super.setValidateTime(trail.getValidateTime());
        super.setValidator_ext(trail.getValidator_ext());
        super.setValidator(trail.getValidator());
        super.setUpload_time(trail.getUpload_time());
        super.setUploader_ext(trail.getUploader_ext());
        super.setUploader(trail.getUploader());
        super.setFinish_time(trail.getFinish_time());
        super.setFinisher_ext(trail.getFinisher_ext());
        super.setFinisher(trail.getFinisher());
    }

    public List<GoodAddressModel> getGdes() {
        return gdes;
    }

    public void setGdes(List<GoodAddressModel> gdes) {
        this.gdes = gdes;
    }

    public String getTrailState() {
        return trailState;
    }

    public void setTrailState(String trailState) {
        this.trailState = trailState;
    }

    public TrailModel(String trailString) {
        fromString(trailString);
    }

    public TrailModel() {
        super();
    }

    /*
     * (non-Javadoc)
     * @see com.mrd.gtimp.business.entity.BaseBusiness#fromString(java.lang.String)
     */
    @Override
    public BaseBusinessModel fromString(String businessString) {
        return null;
    }

    /*
     * (non-Javadoc)
     * @see com.mrd.gtimp.business.entity.BaseBusiness#toString()
     */
    @Override
    public String toString() {
        return null;
    }

    public String getSo() {
        return so;
    }

    public void setSo(String so) {
        this.so = so;
    }

    public String getPi() {
        return pi;
    }

    public void setPi(String pi) {
        this.pi = pi;
    }

    public String getContainerTotle() {
        return containerTotle;
    }

    public void setContainerTotle(String containerTotle) {
        this.containerTotle = containerTotle;
    }

    public String getDeclarationCount() {
        return declarationCount;
    }

    public void setDeclarationCount(String declarationCount) {
        this.declarationCount = declarationCount;
    }

    public String getReqNo() {
        return reqNo;
    }

    public void setReqNo(String reqNo) {
        this.reqNo = reqNo;
    }

    public String getCommonTemplate() {
        return commonTemplate;
    }

    public void setCommonTemplate(String commonTemplate) {
        this.commonTemplate = commonTemplate;
    }

    public String getCommonTemplateName() {
        return commonTemplateName;
    }

    public void setCommonTemplateName(String commonTemplateName) {
        this.commonTemplateName = commonTemplateName;
    }

    public Attachment getSoDoc() {
        return soDoc;
    }

    public void setSoDoc(Attachment soDoc) {
        this.soDoc = soDoc;
    }

    @Override
    public void clearPk() throws Exception {
        PkConfig[] configs = {new PkConfig("id", String.class),
                new PkConfig("CREATOR", String.class), new PkConfig("CTIME|O", Date.class),
                new PkConfig("MODIFIER", String.class), new PkConfig("MTIME|O", Date.class),
                new PkConfig("CONFIRM_TIME|O", Date.class),
                new PkConfig("VALIDATE_TIME|O", Date.class),
                new PkConfig("validator_ext", String.class),
                new PkConfig("validator|O", User.class), new PkConfig("upload_time|O", Date.class),
                new PkConfig("uploader_ext", String.class), new PkConfig("uploader|O", User.class),
                new PkConfig("finish_time|O", Date.class),
                new PkConfig("finisher_ext", String.class), new PkConfig("finisher|O", User.class),
                new PkConfig("so", String.class), new PkConfig("trailState", String.class),
                new PkConfig("soDoc|O", Attachment.class)};
        PkCleanerUtil.clearPk(this, configs);
        for (GoodAddressModel ga : gdes) {
            PkConfig[] gconfigs = {new PkConfig("id", String.class),
                    new PkConfig("trail|O", TrailModel.class),
                    new PkConfig("carmsges|O", List.class)};
            PkCleanerUtil.clearPk(ga, gconfigs);
            /*
			 * for(Container c : ga.getContainers()) {
			 * PkConfig[] cconfigs = {new PkConfig("id", String.class),
			 * new PkConfig("booking|O", Booking.class),
			 * new PkConfig("carMsg|O", CarMsg.class) };
			 * //PkCleanerUtil.clearPk(c, cconfigs);
			 * }
			 */
        }
    }

}
